const db = require("../db/index");

const bcrypt = require("bcryptjs");
// 获取用户信息

/**
 *
 * @param {*} req
 * @param {*} res response_num total_cards_number
 */
exports.getUserInfo = (req, res) => {
  // 根据id查用户
  // 防止用户密码泄露，排除password字段

  const sql = `select age ,id,sign,user_pic,username ,gender ,moment from users where id =?`;
  const sql2 = `select 
  (select count(*) from lost_help where user_id = ? and is_delete <> 1) + 
  (select count(*) from request where user_id = ? and is_delete <> 1) +
  (select count(*) from wall where user_id = ? and is_delete <> 1) as total_cards_number
`;
  const sql3 = `select count(*) as response_num from request_comment where user_id = ? and is_delete !=1`;
  /**
   * req.user 属性是Token 解析成功后带上去的 会自动生成id标识符
   */

  db.query(sql, [req.user.id], (err, results) => {
    if (err) return res.cc(err);
    if (results.length !== 1) return res.cc("获取用户信息失败!");
    // 成功返回 用户信息

    db.query(sql2, [req.user.id, req.user.id, req.user.id], (err, result) => {
      if (err) return res.cc(err);
      db.query(sql3, [req.user.id], (err, result3) => {
        if (err) return res.cc(err);
        let userInfo = {
          ...results[0],
          ...result[0],
          ...result3[0],
        };
        res.send({
          status: 0,
          messaag: "获取用户信息成功",
          data: userInfo,
        });
      });
    });
  });
};

// 更新用户信息
/**
 *
 * @param {body -- username id } req
 * @param {*} res
 */

exports.updateUserInfo = (req, res) => {
  const sql = `update users set ? where id = ?`;
 
  db.query(sql, [req.body, req.user.id], (err, results) => {
    if (err) return res.cc(err);

    if (results.affectedRows !== 1) return res.cc("修改用户基本信息失败！");

    return res.cc("修改用户基本信息成功！", 0);
  });
};

//重置密码
/**
 *
 * @param {oldPwd newPwd } req
 * @param {*} res
 */
exports.updatePassword = (req, res) => {
  const sql = `select * from users where id =  ?`;
  db.query(sql, req.user.id, (err, results) => {
    if (err) return res.cc(err);
    if (results.length !== 1) return res.cc("用户不存在");
    // 判断是否输入旧密码正确
    const compareResult = bcrypt.compareSync(
      req.body.oldPwd,
      results[0].password
    );
    if (!compareResult) return res.cc("原密码错误！");
    // 否则正确 对密码进行加密 更新到数据库
    const sql2 = `update users set password = ? where id = ?`;
    const newPwd = bcrypt.hashSync(req.body.newPwd, 10);
    db.query(sql2, [newPwd, req.user.id], (err, result) => {
      if (err) return res.cc(err);
      if (result.affectedRows !== 1) return res.cc("更新密码失败！");
      res.cc("更新密码成功！", 0);
    });
  });
};
/**
 *
 * @param {user_id} req
 * @param {*} res
 */
exports.findUserInfo = (req, res) => {
  // 根据id查用户
  // 防止用户密码泄露，排除password字段
 
  const sql = `select age ,id,sign,user_pic,username ,gender ,moment from users where id = ?`;
  const sql2 = `select 
   (select count(*) from lost_help where user_id = ? and is_delete != 1) + 
   (select count(*) from request where user_id = ? and is_delete != 1) +
   (select count(*) from wall where user_id = ? and is_delete != 1) as total_cards_number`;
  const sql3 = `select count(*) as response_num from request_comment where user_id = ? and is_delete !=1`;
  /**
   * req.user 属性是Token 解析成功后带上去的 会自动生成id标识符
   */

  db.query(sql, [req.query.user_id], (err, results) => {
    if (err) return res.cc(err);
    if (results.length !== 1) return res.cc("获取用户信息失败!");
    // 成功返回 用户信息

    db.query(
      sql2,
      [req.query.user_id, req.query.user_id, req.query.user_id],
      (err, result) => {
        if (err) return res.cc(err);
        db.query(sql3, [req.query.user_id], (err, result3) => {
          if (err) return res.cc(err);
          let userInfo = {
            ...results[0],
            ...result[0],
            ...result3[0],
          };
          res.send({
            status: 0,
            messaag: "获取用户信息成功",
            data: userInfo,
          });
        });
      }
    );
  });
};
